<?php

/**
 * @Author: king
 * @Date:   2019-05-04 23:13:46
 * @Last Modified by:   king
 * @Last Modified time: 2019-05-04 23:15:17
 */
namespace App\Http\Controllers\Api\v3;


use App\Http\Controllers\Api\BaseController;
use App\Exceptions\ApiException;
use App\Models\Api\Recode;
use App\Models\Api\LanmeiGoodsCommodityAttr;
use App\Models\Api\LanmeiOrder;

/**
 * 检查订单购买状态
 * Class CheckOrders
 * @package app\api\controller\v10
 */
class CheckOrdersController extends BaseController
{

    /**
     * 检查当前档案是否解锁了当前时间
     * @param array $map
     * @return bool
     * @throws ApiException
     */
    public function check_prepaid_fortune($map = [])
    {
        $uid = $map['uid'];
        $rid = $map['rid'];
        $time = strtotime($map['time']);
        $fortune_type = $map['fortune_type'];
        $order_type = $map['order_type'];
        //获取当前档案解锁的所有时间
        $LanmeiOrder = new LanmeiOrder();
        $RecodeModel = new Recode();
        $unlock_time = $LanmeiOrder->getRidprepaidFortune(['uid' => $uid, 'rid' => $rid, 'fortune_type' => $fortune_type, 'order_type' => $order_type]);
        if (in_array($time, $unlock_time)) {
            //获取当前档案的信息
            $recode = $RecodeModel->getRecode(['id' => $rid]);
            throw new ApiException($recode['name'] . '已购买当前时间的运势', 200, config('code.error'));
        }

        return true;
    }

    /**
     * 检查当前档案当前时间是否存在未支付订单
     * @param array $map
     * @return bool
     */
    public function check_unpaid_fortune($map = [])
    {
        $uid = $map['uid'];
        $rid = $map['rid'];
        $time = strtotime($map['time']);
        $fortune_type = $map['fortune_type'];
        $order_type = $map['order_type'];
        //获取当前档案未支付的运势时间
        $LanmeiOrder = new LanmeiOrder();
        $fortune_time = $LanmeiOrder->getRidUnpaidFortune(['uid' => $uid, 'rid' => $rid, 'fortune_type' => $fortune_type, 'order_type' => $order_type]);

        foreach ($fortune_time as $ftime) {
            if ($ftime == $time) {
                //修改订单为已删除
                $LanmeiOrder::update(['status' => 1], ['id' => $ftime['id']]);
            }
        }
        return true;
    }

    /**
     * 获取当前档案解锁的命格线
     * @param array $map
     * @return bool
     * @throws ApiException
     */
    public function check_prepaid_map($map = [])
    {
        $rid = $map['rid'];
        $gctid = $map['gctid'];
        $uid = $map['uid'];
        $order_type = $map['order_type'];
        //获取当前档案解锁的所有时间
        $LanmeiOrder = new LanmeiOrder();
        $RecodeModel = new Recode();
        $LanmeiGoodAttr = new LanmeiGoodsCommodityAttr();
        $unlock_gctid = $LanmeiOrder->getRidPrepaidMap(['uid' => $uid, 'rid' => $rid, 'order_type' => $order_type]);
        if (in_array($gctid, $unlock_gctid)) {
            //获取当前档案的信息
            $recode = $RecodeModel->getRecode(['id' => $rid]);
            //获取当前命格线信息
            $good = $LanmeiGoodAttr->getCommodityInfo(['id' => $gctid]);
            throw new ApiException($recode['name'] . '已购买' . $good['name'], 200, config('code.error'));
        }

        return true;
    }

    /**
     * 获取当前用户未支付的命格线信息
     * @param array $map
     * @return bool
     */
    public function check_unpaid_map($map = [])
    {
        $rid = $map['rid'];
        $gctid = $map['gctid'];
        $uid = $map['uid'];
        $order_type = $map['order_type'];
        //获取当前档案解锁的所有时间
        $LanmeiOrder = new LanmeiOrder();
        $map_good = $LanmeiOrder->getRidUnpaidMap(['uid' => $uid, 'rid' => $rid, 'order_type' => $order_type]);
        foreach ($map_good as $m) {
            if ($m['gctid'] == $gctid) {
                //修改订单为已删除
                $LanmeiOrder::update(['status' => 1], ['id' => $map_good['id']]);
            }
        }
        return true;
    }

    /**
     * 检查当前用户是否解锁了当前课程
     * @param array $map
     * @return bool
     * @throws ApiException
     */
    public function check_prepaid_video($map = [])
    {
        $uid = $map['uid'];
        $gctid = $map['gctid'];
        $order_type = $map['order_type'];
        //获取当前档案解锁的所有时间
        $LanmeiOrder = new LanmeiOrder();
        $unlock_gctid = $LanmeiOrder->getUserPrepaidVideo(['uid' => $uid, 'order_type' => $order_type]);
        if (in_array($gctid, $unlock_gctid)) {
            throw new ApiException('您已购买当前课程', 200, config('code.error'));
        }

        return true;

    }

    /**
     * 检查当前课程用户是否有未支付订单
     * @param array $map
     * @return bool
     */
    public function check_unpaid_video($map = [])
    {
        $uid = $map['uid'];
        $gctid = $map['gctid'];
        $order_type = $map['order_type'];
        //获取当前档案解锁的所有时间
        $LanmeiOrder = new LanmeiOrder();
        $map_good = $LanmeiOrder->getUserUnpaidVideo(['uid' => $uid, 'order_type' => $order_type]);
        foreach ($map_good as $m) {
            if ($m['gctid'] == $gctid) {
                //修改订单为已删除
                $LanmeiOrder::update(['status' => 1], ['id' => $map_good['id']]);
            }
        }
        return true;
    }

}
